package com.hy.study.mapper;

import com.hy.study.bean.FlowBean;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author huyong
 * @date 2024 年 01 月 29 日 16:36: 31
 */
public class FlowCountMapper extends Mapper<LongWritable, Text, Text, FlowBean> {
    private final Text text = new Text();
    private final FlowBean flowBean = new FlowBean();

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, FlowBean>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] fileds = line.split("\t");
        int size = fileds.length;
        // 封装对象
        text.set(fileds[1]);
        String filed = fileds[size - 3];
        if (StringUtils.isNotBlank(filed) && StringUtils.isNumeric(filed)) {
            flowBean.setUpFlow(Long.parseLong(filed));
        }
        filed = fileds[size - 2];
        if (StringUtils.isNotBlank(filed) && StringUtils.isNumeric(filed)) {
            flowBean.setDownFlow(Long.parseLong(filed));
        }
        flowBean.setSumFlow(flowBean.getUpFlow() + flowBean.getDownFlow());
        context.write(text, flowBean);
    }
}
